package org.shuma.goods.controller;


import com.alipay.sofa.runtime.api.annotation.SofaReference;
import com.alipay.sofa.runtime.api.annotation.SofaReferenceBinding;
import org.shuma.goods.service.ProductBasicInfoServiceByXingWang;
import org.shuma.proxy.facade.Proxy;
import org.shuma.proxy.facade.entity.ProductProxy;
import org.shuma.proxy.facade.entity.Result;
import org.shuma.user.facade.UserFacade;
import org.shuma.user.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/proxy")
public class ProxyController {
    @SofaReference(interfaceType = Proxy.class, uniqueId = "${service.proxy.unique.id}",
            binding = @SofaReferenceBinding(bindingType = "bolt"))
    private Proxy productProxyService ;
    @SofaReference(interfaceType = UserFacade.class, uniqueId = "${service.user.unique.id}",
            binding = @SofaReferenceBinding(bindingType = "bolt"))
    private UserFacade userFacade;

    @Autowired
    private ProductBasicInfoServiceByXingWang productBasicInfoServiceByXingWang;
    //获取代理人
    @GetMapping("")
    public Result getProxy(@RequestParam("productId") Long productId,
                           @RequestParam(defaultValue = "0") int page,
                           @RequestParam(defaultValue = "10") int size,
                           @RequestAttribute("userInfo")UserVo userVo) {

        if(userVo.getUserRole()!=0){
            return Result.error("该角色无权限");
        }
        if (productId == null) {
            return Result.error("操作失败");
        }
        PageRequest pageRequest = PageRequest.of(page, size);
        Page<ProductProxy> productProxies = productProxyService.queryByPage(productId, pageRequest);
        return Result.success(productProxies);
    }

    //删除代理人
    @DeleteMapping
    public Result delete(@RequestBody ProductProxy productProxy,
                         @RequestAttribute("userInfo")UserVo userVo) {
        long id = userVo.getId();
        long creatorId = productBasicInfoServiceByXingWang.getCreatorId(productProxy.getProductId());
        if (id != creatorId){
            return Result.error("权限不足");
        }
        if(userVo.getUserRole()!=0){
            return Result.error("该角色无权限");
        }
        long productId = productProxy.getProductId();
        String proxyIds = productProxy.getProxyIds();
        if (productId == 0 || proxyIds.isEmpty() ){
            return Result.error("操作失败");
        }
        productProxyService.update(productId, proxyIds);
        return Result.success();
    }
    //指定代理人
    @PostMapping("/appointproxy")
    public Result addProxy(@RequestBody ProductProxy productProxy,
                           @RequestAttribute("userInfo")UserVo userVo) {

        if(userVo.getUserRole()!=0){
            return Result.error("该角色无权限");
        }
        long id = userVo.getId();
        long creatorId = productBasicInfoServiceByXingWang.getCreatorId(productProxy.getProductId());
        if (id != creatorId){
            return Result.error("该角色无权限");
        }
        long proxyId = productProxy.getProxyId();
        if (id == proxyId){
            return Result.error("不能添加自己为代理人");
        }
        List<ProductProxy> productProxyList = productProxyService.queryById(productProxy.getProductId());
        List<Long> proyxIdlist = new ArrayList<>();
        for (ProductProxy proxy : productProxyList) {
            proyxIdlist.add(proxy.getProxyId())  ;
        }
        if (proyxIdlist.contains(productProxy.getProxyId())){
            return Result.error("该代理人已存在,请勿重复添加");
        }
        Long productId = productProxy.getProductId();
        if (productId == null|| proxyId == 0){
            return Result.error("操作失败");
        }
        productProxyService.addProductProxy(productProxy);
        return Result.success();
    }
}
